<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="业务类型">
              <!--              <a-input placeholder="请输入分类名称" v-model="queryParam.name"></a-input>-->
              <!--              <j-dict-select-tag type="list" v-model="queryParam.businessType"-->
              <!--                                 dictCode="business_type" placeholder="请选择业务类型"/>-->
              <j-dict-select-tag
                ref="dictSelect"
                type="list"
                v-model="queryParam.businessType"
                placeholder="请选择业务类型"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('成品入库单')">导出</a-button>
      <!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">-->
      <!--        <a-button type="primary" icon="import">导入</a-button>-->
      <!--      </a-upload>-->
      <!-- 高级查询区域 -->
      <j-super-query
        :fieldList="superFieldList"
        ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"
      ></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        
        rowKey="id"
        class="j-table-force-nowrap1"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :loading="loading"
        :customRow="orderRowProperties"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange ,type:getType}"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text, record">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            :preview="record.id"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
      <a-table
        ref="table"
        size="middle"
        bordered
        :rowKey="
          (record, index) => {
            return record.detailId
          }
        "
        class="j-table-force-nowrap1"
        :columns="noticeDetailsTable.columns"
        :dataSource="noticeDetailsTable.dataSource"
        :pagination="ipagination"
        :loading="loading"
        :scroll="{ x: true }"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange"
        :customRow="rowProperties"
        :rowClassName="(record, index) => (record.qcStatus == 2 ? 'qc' : null)"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
      </a-table>
    </div>
    <buss-spare-part-storage-modal ref="modalForm" @ok="modalFormOk" />
    <erp-spare-part-modal ref="sparePartDetailModal" @ok="modalFormOk" />
  </a-card>
</template>

<script>
import { deleteAction, getAction, putAction } from '@api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { Message, MessageBox, Notification, Loading } from 'element-ui'
import ErpSparePartForm from './modules/ErpSparePartForm'
import BussSparePartStorageModal from './modules/BussSparePartStorageModal'
import ErpSparePartModal from './modules/ErpSparePartModal'
import '@/assets/less/TableExpand.less'

export default {
  name: 'BussSparePartStorageList',
  mixins: [JeecgListMixin],
  components: {
    BussSparePartStorageModal,
    ErpSparePartModal,
    ErpSparePartForm,
  },
  data() {
    return {
      description: '成品入库单管理页面',
      // 表头
      settingColumns: [],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '入库类型',
          align: 'center',
          dataIndex: 'storageType_dictText',
        },
        {
          title: '入库单号',
          align: 'center',
          dataIndex: 'code',
        },
        {
          title: '入库备注',
          align: 'center',
          dataIndex: 'remark',
        },
        {
          title: '仓库名称',
          align: 'center',
          dataIndex: 'warehouseName',
        },

        {
          title: '上级单据号',
          align: 'center',
          dataIndex: 'parentCode',
        },
        {
          title: '经办人员',
          align: 'center',
          dataIndex: 'dealUsername',
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'createTime',
        },
        // {
        //   title: '经办部门',
        //   align: 'center',
        //   dataIndex: 'sysOrgCode',
        // },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'createBy',
        },
        // {
        //   title: '制单部门',
        //   align: 'center',
        //   dataIndex: 'sysOrgCode',
        // },
        {
          title: '入库日期',
          align: 'center',
          dataIndex: 'storageDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '供应商',
          align: 'center',
          dataIndex: 'supplier',
        },
        {
          title: '生产车间',
          align: 'center',
          dataIndex: 'workshop',
        },
        {
          title: '客户名称',
          align: 'center',
          dataIndex: '',
        },
        {
          title: '批次号',
          align: 'center',
          dataIndex: 'lotNumber',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          customRender: (text, record, index) => {
            const obj = {
              children: [
                <a onClick={() => this.handleEdit(record)}>编辑</a>,
                <a-divider type="vertical"></a-divider>,
                <a-dropdown>
                  <a class="ant-dropdown-link">
                    更多 <a-icon type="down" />
                  </a>
                  <a-menu slot="overlay">
                    <a-menu-item>
                      <a onClick={() => this.handleDetail(record)}>详情</a>
                    </a-menu-item>
                    <a-menu-item>
                      <a-popconfirm title="确定删除吗?" onConfirm={() => this.handleDelete(record.id)}>
                        <a>删除</a>
                      </a-popconfirm>
                    </a-menu-item>
                  </a-menu>
                </a-dropdown>,
              ],
              attrs: {},
            }
            // 判断是否需要合并行
            if (index > 0 && record.id === this.dataSource[index - 1].id) {
              obj.attrs.rowSpan = 0
            } else {
              obj.attrs.rowSpan = this.dataSource.filter((item) => item.id === record.id).length
            }
            return obj
          },
          // scopedSlots: { customRender: 'action' },
        },
      ],
      noticeDetailsTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function (t, r, index) {
              return parseInt(index) + 1
            },
          },
          {
            title: '物料编号',
            align: 'center',
            dataIndex: 'materialCode',
            scopedSlots: { customRender: 'htmlSlot' },
          },
          {
            title: '物料名称',
            align: 'center',
            dataIndex: 'materialName',
            scopedSlots: { customRender: 'htmlSlot' },
          },
          {
            title: '规格',
            align: 'center',
            dataIndex: 'spec',
            scopedSlots: { customRender: 'htmlSlot' },
          },
          {
            title: '库位名称',
            align: 'center',
            dataIndex: 'erpLocationName',
            scopedSlots: { customRender: 'htmlSlot' },
          },
          {
            title: '生产车间',
            align: 'center',
            dataIndex: 'workshop',
            scopedSlots: { customRender: 'htmlSlot' },
          },
          {
            title: '原发数量',
            align: 'center',
            dataIndex: 'originalNum',
            scopedSlots: { customRender: 'htmlSlot' },
          },
          {
            title: '实收数量',
            align: 'center',
            dataIndex: 'actualNum',
            scopedSlots: { customRender: 'htmlSlot' },
          },
          {
            title: '批次号',
            align: 'center',
            dataIndex: 'batchNum',
            scopedSlots: { customRender: 'htmlSlot' },
          },
          {
            title: '物料批号',
            align: 'center',
            dataIndex: 'materialLotNum',
            scopedSlots: { customRender: 'htmlSlot' },
          },
          {
            title: '客户物料编号',
            align: 'center',
            dataIndex: 'customerMaterialNo',
            scopedSlots: { customRender: 'htmlSlot' },
          },
          {
            title: '生产日期',
            align: 'center',
            dataIndex: 'createDate',
            scopedSlots: { customRender: 'htmlSlot' },
          },

          {
            title: '限用日期',
            align: 'center',
            dataIndex: 'expiryDate',
            scopedSlots: { customRender: 'htmlSlot' },
          },

          {
            title: '采购数量',
            align: 'center',
            dataIndex: 'procurementNum',
            scopedSlots: { customRender: 'htmlSlot' },
          },
          {
            title: '已到货数量',
            align: 'center',
            dataIndex: 'arrivalNum',
            scopedSlots: { customRender: 'htmlSlot' },
          },

          {
            title: '单位',
            key: 'meUnit',
            width: '70px',
            dataIndex: 'sysOrgCode',
            // disabled: true
          },
          {
            title: '供应商',
            key: 'supplierId',
            width: '200px',
            dataIndex: 'supplierId',
            slotName: 'supplierId',
          },
          {
            title: '采购订单编号',
            key: 'procurementCode',
            width: '120px',
            dataIndex: 'procurementCode',
          },
          {
            title: '排产单号',
            key: 'schedulingCode',
            width: '120px',
            dataIndex: 'schedulingCode',
          },

          {
            title: '辅助数量',
            key: 'supportNum',
            width: '120px',
            dataIndex: 'supportNum',
          },
          {
            title: '辅助单位',
            key: 'supportUnit',
            width: '70px',
            dataIndex: 'supportUnit',
          },
          {
            title: '当前状态',
            key: 'status',
            width: '70px',
            placeholder: '请输入${title}',
            defaultValue: '',
            dataIndex: 'status',
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            fixed: 'right',
            width: 150,
            customRender: (text, record, index) => {
              const obj = {
                children: [
                  //record.status === 0 || record.status === 2
                  <a onClick={() => this.sparePartDetail(record)}>查看详情</a>,
                ],
                attrs: {},
              }

              return obj
            },
            // scopedSlots: { customRender: 'action' },
          },
        ],
        dataSource: [],
        dataSourceBack: [],
        ipagination: {
          current: 1,
          pageSize: 30,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      url: {
        list: '/business/bussSparePartStorage/page',
        delete: '/business/bussSparePartStorage/delete',
        deleteBatch: '/business/bussSparePartStorage/deleteBatch',
        exportXlsUrl: '/business/bussSparePartStorage/exportXls',
        importExcelUrl: 'business/bussSparePartStorage/importExcel',
        queryBussSparePartStorageDetailByMainId: {
          list: '/business/bussSparePartStorage/queryBussSparePartStorageDetailByMainId',
          generateBarCode: '/business/bussSparePartStorage/generateBarCode',
        },
        getGoodsById: '/base/erpGoods/queryById',
      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {},
  mounted() {},
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
        getType: function () {
      return 'checkbox'
    },
  },
  methods: {
    sparePartDetail(record) {
      getAction(this.url.getGoodsById, { id: record.materialId }).then((res) => {
        if (res.success) {
      this.$refs.sparePartDetailModal.edit(res.result)
      this.$refs.sparePartDetailModal.title = '详情'
      this.$refs.sparePartDetailModal.disableSubmit = true
        }
      })
      
    },
    //主列表选中执行方法
    selectedRowKeys(row) {},
    barcodeListFormOpen(record) {
      this.$refs.sparePartDetailModal.visible = true
    },
    rowProperties(record, index) {
      return {
        on: {
          click: (event) => {
            // alert(888888)
          },
        },
        // onClick: (event) => {
        //   alert(888888)
        // },
      }
    },
    orderRowProperties(record, index) {
      return {
        on: {
          click: (event) => {
            console.log(record)

            this.getDetailByNoticeId(record.id)
          },
        },
        // onClick: (event) => {
        //   alert(888888)
        // },
      }
    },
    //获取入库详情
    getDetailByNoticeId(id) {
      let params = { id: id }
      this.noticeDetailsTable.dataSource = []
      getAction(this.url.queryBussSparePartStorageDetailByMainId.list, params).then((res) => {
        this.noticeDetailsTable.dataSource = res.result
        console.log('---------------------')
        console.log(res.result)
      })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>